<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户添加</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 添加用户表单 -->
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addFormRef"
        label-width="100px"
        label-position="top"
      >
        <el-form-item label="用户名:" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱:" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input v-model="addForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="角色:" prop="role">
          <!-- 选择器 -->
          <el-select v-model="addForm.role" placeholder="请选择">
            <el-option
              label="超级管理员"
              value="admin"
            ></el-option>
            <el-option
              label="老师"
              value="teacher"
            ></el-option>
            <el-option
              label="学生"
              value="student"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <!-- 确认提交按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="addUser">提交</el-button>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    // 自定义表单验证规则
    var emailCheck = (rule, value, cb) => {
      // 邮箱验证正则表达式
      const emailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
      if (emailReg.test(value)) {
        // 如果通过验证，return cb()
        return cb()
      }
      // 否则在cb里面new 一个 Error，传入错误信息
      cb(new Error('邮箱格式不符合规则,请重新输入'))
    }
    return {
      // 添加用户表单对象
      addForm: {
        username: '',
        email: '',
        password: '',
        role: ''
      },
      // 表单验证规则
      addRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { validator: emailCheck, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 16, message: '请输入3-16位密码', trigger: 'blur' }
        ],
        role: [{ required: true, message: '请输入选择角色', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 提交表单
    addUser() {
      // 表单预验证
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('验证不通过，请重新输入')
        }
        // 验证通过，发送请求
        const { data: res } = await this.$http.post('/users/add', this.addForm)
        console.log(res)
        if (res.meta.status === 400) {
          return this.$message.error('添加失败，邮箱地址已经存在')
        }
        // 添加成功的话,给出提示
        this.$message.success('添加用户成功')
        // 跳转页面回用户列表
        this.$router.push('/users')
      })
    }
  }
}
</script>
